package com.ruoyi.dataBase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.dataBase.domain.IndustrialFunctDept;
import com.ruoyi.dataBase.domain.IndustrialReportObj;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dataBase.domain.bo.IndustrialTransportPlanBo;
import com.ruoyi.dataBase.domain.vo.IndustrialTransportPlanVo;
import com.ruoyi.dataBase.domain.IndustrialTransportPlan;
import com.ruoyi.dataBase.mapper.IndustrialTransportPlanMapper;
import com.ruoyi.dataBase.service.IIndustrialTransportPlanService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 交通物流规划Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-10
 */
@RequiredArgsConstructor
@Service
public class IndustrialTransportPlanServiceImpl implements IIndustrialTransportPlanService {

    private final IndustrialTransportPlanMapper baseMapper;

    /**
     * 查询交通物流规划
     */
    @Override
    public IndustrialTransportPlanVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询交通物流规划列表
     */
    @Override
    public TableDataInfo<IndustrialTransportPlanVo> queryPageList(IndustrialTransportPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialTransportPlan> lqw = buildQueryWrapper(bo);
        Page<IndustrialTransportPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询交通物流规划列表
     */
    @Override
    public List<IndustrialTransportPlanVo> queryList(IndustrialTransportPlanBo bo) {
        LambdaQueryWrapper<IndustrialTransportPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialTransportPlan> buildQueryWrapper(IndustrialTransportPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialTransportPlan> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), IndustrialTransportPlan::getPlanName, bo.getPlanName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanType()), IndustrialTransportPlan::getPlanType, bo.getPlanType());
        lqw.eq(bo.getPlanSendDate() != null, IndustrialTransportPlan::getPlanSendDate, bo.getPlanSendDate());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanDeadline()), IndustrialTransportPlan::getPlanDeadline, bo.getPlanDeadline());
        lqw.like(StringUtils.isNotBlank(bo.getPreparedBy()), IndustrialTransportPlan::getPreparedBy, bo.getPreparedBy());
        lqw.like(StringUtils.isNotBlank(bo.getMainEntity()), IndustrialTransportPlan::getMainEntity, bo.getMainEntity());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), IndustrialTransportPlan::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IndustrialTransportPlan::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IndustrialTransportPlan::getLatitude, bo.getLatitude());
        lqw.eq(bo.getDataStatus() != null, IndustrialTransportPlan::getDataStatus, bo.getDataStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList1()), IndustrialTransportPlan::getFileList1, bo.getFileList1());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList2()), IndustrialTransportPlan::getFileList2, bo.getFileList2());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList3()), IndustrialTransportPlan::getFileList3, bo.getFileList3());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList4()), IndustrialTransportPlan::getFileList4, bo.getFileList4());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList5()), IndustrialTransportPlan::getFileList5, bo.getFileList5());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList6()), IndustrialTransportPlan::getFileList6, bo.getFileList6());

        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), IndustrialTransportPlan::getCreateBy, bo.getCreateBy());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateBy()), IndustrialTransportPlan::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            IndustrialTransportPlan::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            IndustrialTransportPlan::getCreateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));

        return lqw;
    }

    /**
     * 新增交通物流规划
     */
    @Override
    public Boolean insertByBo(IndustrialTransportPlanBo bo) {
        IndustrialTransportPlan add = BeanUtil.toBean(bo, IndustrialTransportPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改交通物流规划
     */
    @Override
    public Boolean updateByBo(IndustrialTransportPlanBo bo) {
        IndustrialTransportPlan update = BeanUtil.toBean(bo, IndustrialTransportPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialTransportPlan entity){
        //TODO 做一些数据校验,如唯一约束
        if (entity == null || StringUtils.isBlank(entity.getPlanName())) {
            throw new IllegalArgumentException("规划名称不能为空");
        }

        LambdaQueryWrapper<IndustrialTransportPlan> queryWrapper = new LambdaQueryWrapper<IndustrialTransportPlan>()
            .eq(IndustrialTransportPlan::getPlanName, entity.getPlanName());

        // 更新时排除当前记录
        if (entity.getId() != null) {
            queryWrapper.ne(IndustrialTransportPlan::getId, entity.getId());
        }

        long count = baseMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new RuntimeException("规划名称已存在");
        }
    }

    /**
     * 批量删除交通物流规划
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
